package ee.ttu.karileet.serviceshop.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import ee.ttu.karileet.serviceshop.bo.CustomerBO;
import ee.ttu.karileet.serviceshop.bo.EmployeeBO;
import ee.ttu.karileet.serviceshop.model.ServiceRequest;
import ee.ttu.karileet.serviceshop.model.ServiceRequestStatusType;
import ee.ttu.karileet.serviceshop.model.UserAccount;
import ee.ttu.karileet.serviceshop.service.CustomerService;
import ee.ttu.karileet.serviceshop.service.EmployeeService;
import ee.ttu.karileet.serviceshop.service.ServiceRequestService;
import ee.ttu.karileet.serviceshop.service.ServiceRequestStatusTypeService;
import ee.ttu.karileet.serviceshop.service.UserAccountService;

@Controller
@RequestMapping(value="/new.htm")
public class NewFormController {

	@Autowired
	private ServiceRequestService serviceRequestService;
	@Autowired
	private ServiceRequestStatusTypeService serviceRequestStatusTypeService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private UserAccountService userAccountService;

	@RequestMapping(method = RequestMethod.GET)
	public String initForm(ModelMap model){
 
		//command object
		model.addAttribute("newServiceRequestForm", new ServiceRequest());
 
		//return form view
		return "new";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView processSubmit(@ModelAttribute("ServiceRequest") ServiceRequest serviceRequest, BindingResult result, SessionStatus status) {
		status.setComplete(); 
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		String creator = authentication.getName();
		UserAccount account = userAccountService.getUser(creator);
		serviceRequest.setCreatedBy(account.getSubjectFk());
		serviceRequest.setCreated(new Date());
		serviceRequestService.insertServiceRequest(serviceRequest);
		return new ModelAndView("requestPreview", "sr", serviceRequest); 
	}
	
	//service_request_status_type
	@ModelAttribute("customers")
	public ArrayList<CustomerBO> getCustomers() {
		return (ArrayList<CustomerBO>) customerService.getCustomers();
	}
	
	@ModelAttribute("employees")
	public ArrayList<EmployeeBO> getEmployees() {
		return (ArrayList<EmployeeBO>) employeeService.getEmployees();
	}
	
	@ModelAttribute("statuses")
	public ArrayList<ServiceRequestStatusType> getStatuses() {
        return (ArrayList<ServiceRequestStatusType>) serviceRequestStatusTypeService.getServiceRequestStatusTypes();
	}
	
	@ModelAttribute("list")
	public ArrayList<ServiceRequest> getServiceRequests() {
        return (ArrayList<ServiceRequest>) serviceRequestService.getServiceRequests();
	}
}
